//
// Created by wujehy on 2020/8/3.
//

#include "ContextManager.h"
#include "innerType.h"

ManagerBase::Type ContextManager::type()
{
    return ManagerType_Context;
}

bool ContextManager::add(int &taskid, ContextBase *context)
{
    _mutex.lock();
    taskid = current_taskid++;

    _mutex.unlock();

    auto pt = m_task_Contxt_map.insert( {taskid, context} );

    return pt.second;
}

void ContextManager::init()
{

}

ContextManager::~ContextManager()
{
    fprintf( stderr, "delete ContextManager \n" );
    for (auto c: m_task_Contxt_map)
    {
        delete c.second;
    }
}

ContextManager::ContextManager()
{
    fprintf( stderr, "init ContextManager \n" );
}

void ContextManager::remove(int taskid)
{
    auto find = m_task_Contxt_map.find( taskid );
    if (find != m_task_Contxt_map.end())
    {
        if (find->second != nullptr)
        {
            delete find->second;
        }
        m_task_Contxt_map.erase( find );
    }
}

void ContextManager::init_finished()
{
    for (auto c : m_task_Contxt_map)
    {
        c.second->init_finished();
    }
}
